/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.ui;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

/**
 * Decorator for applying an alpha composite to another {@link JComponent}.
 * <p>
 * The fade factor
 * 
 * @author Peter De Bruycker
 */
public class AlphaCompositeDecorator extends JComponent {

    private static final long serialVersionUID = 1L;

    private final float alpha;

    public AlphaCompositeDecorator(JComponent delegate, float alpha) {
        setLayout(new BorderLayout());
        add(delegate);

        this.alpha = alpha;
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D graphics = (Graphics2D) g.create();

        graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

        super.paint(graphics);

        graphics.dispose();
    }
}
